package com.university.logistic.uitl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime2String {

    public static String dateTime2String() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    //增加了一个对当前时间增加一年的方法（这里主要用于新增管理员是，有一个签约到期时间字段）
    public static String getExpireTime(String signingTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date signingDate = sdf.parse(signingTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(signingDate);
            // 增加 1 年
            calendar.add(Calendar.YEAR,  1);
            Date expireDate = calendar.getTime();
            return sdf.format(expireDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Date string2Date(String dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    public static String calculateServiceTime(String orderStart, String orderEnd) {
        //将字符串转换为日期对象
        Date orderStartDate = string2Date(orderStart);
        Date orderEndDate = string2Date(orderEnd);
        //计算几小时几分钟
        long diff = orderEndDate.getTime() - orderStartDate.getTime();
        if (diff < 0) throw new IllegalArgumentException("结束时间不能早于开始时间");
        long hours = diff / (1000 * 60 * 60);
        long minutes = (diff % (1000 * 60 * 60)) / (1000 * 60);
        String duration = hours + "小时" + minutes + "分钟";
        return duration;
    }

    /**
     * 好友聊天记录的时间
     * @param time
     * @return
     */
    public static String chatLastDate(String time){
        long timestamp = Long.parseLong(time); // 转为 long
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String formattedDate = sdf.format(new Date(timestamp));
        return formattedDate;
    }

}
